package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.RequestBean;
import com.attendance.www.hr.beans.RequestPageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IRequestService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 休假申请
 * @author 张鑫
 * 2017-09-29
 *
 */
@Controller
@RequestMapping("/request")
public class RequestController {

	@Resource
	private IRequestService requestService;
	
	/**
	 * 增加一条休假（或出差或外出）申请
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/saveRequest.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object saveRequest(HttpServletRequest request,@RequestBody RequestBean requestBean){
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//申请开关
		boolean applicationType=Boolean.parseBoolean(AppConstant.APPLICATION_TYPE);
		if(applicationType) {
			throw new ResultFaultException("4_00027");
		}
		
		requestBean.setEmployeeId(userEmployee.getEmployeeId());
		if(requestBean!=null){
			if (requestBean.getStartDate() == null
				||requestBean.getRequestType() == null) {
				throw new ResultFaultException("0_00001");
			}
			if(requestService.verifySaveRequestClosed(requestBean)){//先封账验证
				throw new ResultFaultException("4_00003");
			} else if(requestService.verifySaveRequest(requestBean)){//是否可申请验证
				throw new ResultFaultException("4_00001");
			} else {
				if (requestService.saveRequest(requestBean)) {
					return ResultMap.success();
				}
			}
		}
		throw new ResultFaultException("0_00007");
	}
	
	/**
	 * 修改一条休假（或出差或外出）申请
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/updateRequest.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object updateRequest(HttpServletRequest request,@RequestBody RequestBean requestBean){
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//申请开关
		boolean applicationType=Boolean.parseBoolean(AppConstant.APPLICATION_TYPE);
		if(applicationType) {
			throw new ResultFaultException("4_00027");
		}
		
		if(requestBean!=null){
			if (requestBean.getStartDate() == null
				||requestBean.getEndDate() == null
				||requestBean.getRequestType() == null
				||requestBean.getRequestId()==null) {
				throw new ResultFaultException("0_00001");
			}
			requestBean.setEmployeeId(userEmployee.getEmployeeId());
			if(requestService.verifySaveRequest(requestBean)){
				throw new ResultFaultException("4_00001");
			} else if(requestService.verifySaveRequestClosed(requestBean)){
				throw new ResultFaultException("4_00003");
			} else{
				if (requestService.updateRequest(requestBean)) {
					return ResultMap.success();
				}
			}
		}
		throw new ResultFaultException("0_00008");
	}
	/**
	 * 删除申请
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteRequest.do", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Object deleteRequest(HttpServletRequest request) {
		String requestIdStr=request.getParameter("requestId");
		if (StringUtils.isBlank(requestIdStr)) {
			throw new ResultFaultException("0_00001");
		}
		Integer requestId=Integer.parseInt(requestIdStr);
		if (requestService.deleteRequest(requestId)) {
			return ResultMap.success();
		}else{
			throw new ResultFaultException("0_00009");
		}
	}
	
	/**
	 * 查询休假申请信息
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/getFurloughRequestList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getFurloughRequestList(HttpServletRequest request,@RequestBody RequestBean requestBean){
		if(requestBean!=null){
			//获取登陆用户数据  
			UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
			if(userEmployee == null){
				throw new ResultFaultException("1_00001");
			}
			requestBean.setEmployeeId(userEmployee.getEmployeeId());
			Integer pageNo =requestBean.getPageNo();
			Integer pageCount = requestBean.getPageCount();
			if (pageNo ==null||pageCount==null||requestBean.getRequestStartDate() == null||requestBean.getRequestEndDate()== null) {
				throw new ResultFaultException("0_00001");
			}
			Integer startNum = 0;
			if (pageNo > 1) {
				startNum = (pageNo - 1) * pageCount; 
			}
			RequestPageBean requestPageBean= new RequestPageBean();
			requestPageBean=requestService.getFurloughRequestList(requestBean, startNum,pageCount);
			return ResultMap.success(requestPageBean);
		}
		throw new ResultFaultException("0_00001");
	}

	/**
	 * 增加一条加班申请
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/saveWorkRequest.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object saveWorkRequest(HttpServletRequest request,@RequestBody RequestBean requestBean){
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//申请开关
		boolean applicationType=Boolean.parseBoolean(AppConstant.APPLICATION_TYPE);
		if(applicationType) {
			throw new ResultFaultException("4_00027");
		}
		
		requestBean.setEmployeeId(userEmployee.getEmployeeId());
		if(requestBean!=null){
			if (requestBean.getStartDate() == null
					|| requestBean.getEndDate() == null
					|| requestBean.getStartTime()==null
					|| requestBean.getEndTime()==null
					|| requestBean.getRequestType() == null
					|| requestBean.getHours()==null) {
				throw new ResultFaultException("0_00001");
			}
			if(requestService.verifySaveRequest(requestBean)){
				throw new ResultFaultException("4_00001");
			} else if(requestService.verifySaveRequestClosed(requestBean)){
				throw new ResultFaultException("4_00003");
			} else{
				if (requestService.saveRequest(requestBean)) {
					return ResultMap.success();
				}
			}
		}
		throw new ResultFaultException("0_00007");
	}
	/**
	 * 查询加班申请信息
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/getWorkRequestList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getWorkRequestList(HttpServletRequest request,@RequestBody RequestBean requestBean){
		if(requestBean!=null){
			//获取登陆用户数据  
			UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
			if(userEmployee == null){
				throw new ResultFaultException("1_00001");
			}
			requestBean.setEmployeeId(userEmployee.getEmployeeId());
			Integer pageNo =requestBean.getPageNo();
			Integer pageCount = requestBean.getPageCount();
			if (pageNo ==null||pageCount==null||requestBean.getRequestStartDate() == null||requestBean.getRequestEndDate()== null||requestBean.getRequestType()==null) {
				throw new ResultFaultException("0_00001");
			}
			Integer startNum = 0;
			if (pageNo > 1) {
				startNum = (pageNo - 1) * pageCount; 
			}
			RequestPageBean requestPageBean= new RequestPageBean();
			requestPageBean=requestService.getWorkRequestList(requestBean, startNum,pageCount);
			return ResultMap.success(requestPageBean);
		}
		throw new ResultFaultException("0_00001");
	}
	/**
	 * 修改一条加班申请
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/updateWorkRequest.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object updateWorkRequest(HttpServletRequest request,@RequestBody RequestBean requestBean){
		if(requestBean!=null){
			if (requestBean.getStartDate() == null
					|| requestBean.getEndDate() == null
					|| requestBean.getRequestType() == null
					|| requestBean.getStartTime()==null
					|| requestBean.getEndTime()==null
					|| requestBean.getHours()==null
					|| requestBean.getRequestId()==null) {
				throw new ResultFaultException("0_00001");
			}
			
			//申请开关
			boolean applicationType=Boolean.parseBoolean(AppConstant.APPLICATION_TYPE);
			if(applicationType) {
				throw new ResultFaultException("4_00027");
			}
			
			if(requestService.verifySaveRequest(requestBean)){
				throw new ResultFaultException("4_00001");
			} else if(requestService.verifySaveRequestClosed(requestBean)){
				throw new ResultFaultException("4_00003");
			} else{
				if (requestService.updateRequest(requestBean)) {
					return ResultMap.success();
				}
			}
		}
		throw new ResultFaultException("0_00008");
	}
	/**
	 * 查询出差或外出申请信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getAwayRequestList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getAwayRequestList(HttpServletRequest request,@RequestBody RequestBean requestBean){
		if(requestBean!=null){
			//获取登陆用户数据  
			UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
			if(userEmployee == null){
				throw new ResultFaultException("1_00001");
			}
			requestBean.setEmployeeId(userEmployee.getEmployeeId());
			Integer pageNo =requestBean.getPageNo();
			Integer pageCount = requestBean.getPageCount();
			if (pageNo ==null||pageCount==null||requestBean.getRequestStartDate() == null||requestBean.getRequestEndDate()== null||requestBean.getRequestType()==null) {
				throw new ResultFaultException("0_00001");
			}
			Integer startNum = 0;
			if (pageNo > 1) {
				startNum = (pageNo - 1) * pageCount; 
			}
			RequestPageBean requestPageBean= new RequestPageBean();
			requestPageBean=requestService.getAwayRequestList(requestBean, startNum, pageCount);
			return ResultMap.success(requestPageBean);
		}
		throw new ResultFaultException("0_00001");
	}
	
	/**
	 * 撤回申请
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getBackRequest.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getBackRequest(HttpServletRequest request){
		String requestIdStr=request.getParameter("requestId");
		if (StringUtils.isBlank(requestIdStr)) {
			throw new ResultFaultException("0_00001");
		}
		Integer requestId=Integer.parseInt(requestIdStr);
		if (requestService.verifyGetBackRequest(requestId)) {
			throw new ResultFaultException("4_00019");
		}
		if (requestService.getBackRequest(requestId)) {
			return ResultMap.success();
		}else{
			throw new ResultFaultException("0_00009");
		}
	}
}
